﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Web.Core;
using mesoBoard.Data;
using mesoBoard.Data.Repositories;

namespace mesoBoard.Web.Areas.Admin.Controllers
{
    public class RolesController : mesoBoardAdminController
    {
        public ActionResult Roles()
        {
            SetCrumb("Roles");
            IEnumerable<Role> TheRoles = Repositories.Roles.GetAllRoles();
            ViewData["RankID"] = new SelectList(Repositories.Ranks.GetRanks().Where(x => x.IsRoleRank == true), "RankID", "Title");
            return View(TheRoles);
        }

        public ActionResult RoleDetails(int RoleID)
        {
            Dictionary<string,byte> specialPermissions = new Dictionary<string,byte>()
            {
                {"None", 0},
                {"Moderator", 1},
                {"Administrator", 2}
            };

            SetCrumb("Roles");
            Role TheRole = Repositories.Roles.GetRole(RoleID);
            ViewData["SpecialPermissions"] = new SelectList(specialPermissions, "Value", "Key", TheRole.SpecialPermissions.ToString()); 
            ViewData["Role"] = TheRole;
            ViewData["RankID"] = new SelectList(Repositories.Ranks.GetRanks().Where(x => x.IsRoleRank == true), "RankID", "Title", TheRole.RankID);
            IEnumerable<User> TheUsers = Repositories.Users.UsersInRole(RoleID);
            return View(TheUsers);
        }

        [HttpPost]
        public ActionResult AddUserToRole(string Username, int RoleID)
        {
            SetCrumb("Roles");
            User TheUser = Repositories.Users.GetUser(Username);
            if (TheUser != null)
            {
                if (!Repositories.Roles.UserInRole(TheUser.UserID, RoleID))
                {
                    Repositories.Roles.AddUserToRole(TheUser.UserID, RoleID);
                    TempData["Success"] = Username + " was added to role";
                }
                else
                {
                    SetError(Username + " is already in role");
                }
            }
            else
            {
                SetError(Username + " is not a valid username");
            }

            return RedirectToAction("RoleDetails", new { RoleID = RoleID });
        }


        public ActionResult RemoveFromRole(int UserID, int RoleID)
        {
            Repositories.Roles.RemoveUserFromRole(UserID, RoleID);
            SetSuccess("User removed from role");
            return RedirectToAction("RoleDetails", new { RoleID = RoleID });

        }

        public ActionResult CreateRole(Role TheRole)
        {
            if (string.IsNullOrEmpty(TheRole.Name))
                SetError("Enter a role name");
            else if (Repositories.Roles.RoleExists(TheRole.Name))
                SetError("Role name is a duplicate");
            else
            {
                Repositories.Roles.CreateRole(TheRole);
                SetSuccess("Role added");
            }
            return RedirectToAction("Roles");
        }

        public ActionResult DeleteRole(int RoleID)
        {
            Repositories.Roles.DeleteRole(RoleID);
            SetSuccess("Role deleted");
            return RedirectToAction("Roles");
        }

        [HttpPost]
        public ActionResult EditRole(Role TheRole)
        {
            Role TestRole = Repositories.Roles.GetRole(TheRole.Name);

            if (TestRole != null && TestRole.RoleID != TheRole.RoleID)
                ModelState.AddModelError("Name", "A role with that name already exists");
            if (string.IsNullOrWhiteSpace(TheRole.Name))
                ModelState.AddModelError("Name", "Enter a role name");

            if (ModelState.IsValid)
            {
                Repositories.Roles.UpdateRole(TheRole);
                SetSuccess("Role updated");
                return RedirectToAction("RoleDetails", new { RoleID = TheRole.RoleID });
            }
            else
            {
                SetError("Validation Error");
                return RedirectToAction("RoleDetails", new { RoleID = TheRole.RoleID });
            }
        }
    }
}
